package com.tinckay.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.common.util.LocalDateTimeUtils;
import com.tinckay.common.util.ShiroUtils;
import com.tinckay.entity.po.Task;
import com.tinckay.service.TaskService;
import io.swagger.annotations.*;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-21
 */
@Api(tags = "运输任务管理")
@RestController
@RequestMapping("/task")
public class TaskController extends ApiController {

    @Autowired
    private TaskService taskService;

    @Log("删除运输任务")
    @DeleteMapping
    @ApiOperation("删除运输任务")
    //@RequiresPermissions("task:delete")
    public R delete(@ApiParam("运输任务id数组") @RequestParam Long[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return failed("参数id不能为空.");
        }
        taskService.removeByIds(CollectionUtil.toList(ids));
        return success(null);
    }

    @Log("更新运输任务")
    @PutMapping
    @ApiOperation("更新运输任务")
    //@RequiresPermissions("task:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) Task task) {
        taskService.updateById(task);
        return success(task);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询运输任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workDate", value = "工作日期(yyyy-MM-dd)", defaultValue = "", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "barnNo", value = "仓体编号", defaultValue = "", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "vehNo", value = "车辆编号", defaultValue = "", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页号", defaultValue = "", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数", defaultValue = "", required = false, dataType = "string", paramType = "query")
    })
    //@RequiresPermissions("task:list")
    public R list(@RequestParam(required = false) String workDate,
                  @RequestParam(required = false) int barnNo,
                  @RequestParam(required = false) int vehNo,
                  @RequestParam(defaultValue = "1") int current,
                  @RequestParam(defaultValue = "10") int size) {
        if (StrUtil.isNotBlank(workDate)) {
            try {
                LocalDateTimeUtils.parseDate(workDate);
            } catch (Exception e) {
                return failed("日期验证有误");
            }
        }

        IPage<Task> page = taskService.page(new Page<>(current, size), new QueryWrapper<Task>().
                lambda()
                .eq(!ShiroUtils.isAdmin(), Task::getUserId, ShiroUtils.getCurrentUserId())
                .likeRight(Task::getStartTime, workDate)
                .eq(Task::getBarnNo, barnNo)
                .eq(Task::getVehNo, vehNo)
        );
        return success(page);
    }

    @GetMapping("/info/{taskId}")
    @ApiOperation("获取任务运输过程信息")
    //@RequiresPermissions("task:info")
    public R info(@PathVariable("taskId") long taskId) {
        return success(taskService.taskInfo(taskId));
    }

    @GetMapping("/tranInfo")
    @ApiOperation("获取运输任务统计信息")
    //@RequiresPermissions("task:tranInfo")
    @ApiImplicitParam(name = "transportTime", value = "运输日期", defaultValue = "", required = true, dataType = "string", paramType = "query")
    public R tranInfo(@RequestParam String transportTime) {
        try {
            LocalDateTimeUtils.parseDate(transportTime);
        } catch (Exception e) {
            return failed("日期验证有误");
        }
        return success(taskService.tranInfo(transportTime));
    }

    @GetMapping("/countTask")
    @ApiOperation("运输任务统计")
    //@RequiresPermissions("task:countTask")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tranTimes", value = "运输次数", defaultValue = "", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "workDate", value = "工作日期(yyyy-MM-dd)", defaultValue = "", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "barnNo", value = "仓体编号", defaultValue = "", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "vehNo", value = "车辆编号", defaultValue = "", required = false, dataType = "string", paramType = "query")
    })
    public R countTask(@RequestParam(value = "tranTimes", required = false) Integer tranTimes,
                       @RequestParam(value = "workDate") String workDate,
                       @RequestParam(value = "barnNo", required = false) String barnNo,
                       @RequestParam(value = "vehNo", required = false) String vehNo) {
        try {
            LocalDateTimeUtils.parseDate(workDate);
        } catch (Exception e) {
            return failed("日期验证有误");
        }
        return success(taskService.countTask(tranTimes, workDate, barnNo, vehNo));
    }


}

